package co.fitstart.entity.credit;

import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.demo2do.core.utils.DateFormatUtils;
import com.demo2do.core.utils.StringUtils;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import co.fitstart.entity.coupon.CreditCouponBatch;
import co.fitstart.entity.credit.product.CreditProduct;
import co.fitstart.entity.order.Order;


@Entity
@Table(name = "credit_transaction")
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class CreditTransaction {

    @Id
    @GeneratedValue
    private Long id;
    
    /** 所属积分*/
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private Credit credit;
    
    /** 来源描述*/
    private String description;
    
    /** 来源id*/
    private Long sourceId;
    
    /** 创建时间*/
    private Date createTime;
    
    /** 月份*/
    private String month;
    
    /** 类型*/
    @Enumerated(EnumType.ORDINAL)
    private CreditType type;
    
    /** 积分数额*/
    private int value;
    
    /**
     * default constructor
     */
    public CreditTransaction(){
    	
    }

    /**
     * 
     * @param credit
     */
    public CreditTransaction (Credit credit) {
        this.credit = credit;
        this.createTime = new Date();
        this.month = DateFormatUtils.format(this.createTime, "yyyy-MM");
    }
    
    /**
     * 
     * @param creditCoupon
     * @return
     */
    public CreditTransaction exchangeCoupon(CreditCouponBatch creditCoupon){
        this.description = StringUtils.connect("兑换:", creditCoupon.getTemplateName());
        this.type = CreditType.DECREASE;
        this.sourceId = creditCoupon.getId();
        this.value = creditCoupon.getCreditValue();
        return this;
    }
    

	/**
	 * @param creditProduct
	 */
	public CreditTransaction exchangeCreditProduct(CreditProduct creditProduct) {
		
		this.description = StringUtils.connect("兑换:", creditProduct.getName());
        this.type = CreditType.DECREASE;
        this.sourceId = creditProduct.getId();
        this.value = creditProduct.getCurrentPrice().intValue();
        return this;
	}
    
    /**
     * 
     * @param order
     * @return
     */
    public CreditTransaction order(Order order) {
        this.description = StringUtils.connect("购买:", order.getProduct().getName(), "获取积分!");
        this.type = CreditType.INCREASE;
        this.sourceId = order.getId();
        this.value = new BigDecimal(order.getPrice()).divide(new BigDecimal(100)).intValue();
        return this;
    }
    
    /**
     * @return the id
     */
    public Long getId() {
        return this.id;
    }

    /**
     * @return the credit
     */
    public Credit getCredit() {
        return this.credit;
    }

    /**
     * @return the description
     */
    public String getDescription() {
        return this.description;
    }

    /**
     * @return the sourceId
     */
    public Long getSourceId() {
        return this.sourceId;
    }

    /**
     * @return the createTime
     */
    public Date getCreateTime() {
        return this.createTime;
    }

    /**
     * @return the month
     */
    public String getMonth() {
        return this.month;
    }

    /**
     * @return the type
     */
    public CreditType getType() {
        return this.type;
    }

    /**
     * @return the value
     */
    public int getValue() {
        return this.value;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param credit the credit to set
     */
    public void setCredit(Credit credit) {
        this.credit = credit;
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @param sourceId the sourceId to set
     */
    public void setSourceId(Long sourceId) {
        this.sourceId = sourceId;
    }

    /**
     * @param createTime the createTime to set
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * @param month the month to set
     */
    public void setMonth(String month) {
        this.month = month;
    }

    /**
     * @param type the type to set
     */
    public void setType(CreditType type) {
        this.type = type;
    }

    /**
     * @param value the value to set
     */
    public void setValue(int value) {
        this.value = value;
    }

    
}
